package com.abacus.lcy.m;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateParser implements ValueParser {

	private static final ValueParser instance = new DateParser();

	private static final Map format = Collections.synchronizedMap(new HashMap());

	public static final SimpleDateFormat getSimpleDateFormat(String arg0) {
		SimpleDateFormat sdf = (SimpleDateFormat) format.get(arg0);
		if (null == sdf) {
			sdf = new SimpleDateFormat(arg0);
			format.put(arg0, sdf);
		}
		return sdf;
	}

	public static ValueParser getInstance() {
		return instance;
	}

	public Object valueOf(Value value) {
		if (null != value && null != value.getValues()) {
			if (null == value.getFormat()) {
				Date date = new Date(Long.parseLong(value.getValues()));
				return date;
			}
			SimpleDateFormat sdf = getSimpleDateFormat(value.getFormat());
			try {
				Date date = sdf.parse(value.getValues());
				return date;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
